// pages/shop_serve/rev_order/receiveOrder.js
var UTILS = require("../../../utils/util.js");
var API = require("../../../config/api.js");

var APP = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: -1,
    ordersNoRecv: [],
    ordersWorking: [],
    ordersNotice: []
  },
  /**
   * 自定义函数
   */
  onClickLeft: function () {
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
      success: function(res){}
    });
  },
  toOrderDetail: function (event) {
    var orderId = event.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: "../../ucenter/order/detail/detail?orderId=" + orderId
    });
  },
  recvOrder: function (event) { 
    var _this = this;
    var url = API.SHOP_SERVE.UPDATE_ORDER;
    var requestParam = {
      id: event.currentTarget.dataset.orderid,
      state: UTILS.ORDER_PAY
    }

    UTILS.httpPost(url, {}, requestParam, function (data) {
      UTILS.showSuccessToast(data.state.msg);
      _this.getOrdersNoRecvFromServer();
      _this.getOrdersWorkingFromServer();
    });
  },
  refuseOrder: function (event) {
    var orderId = event.currentTarget.dataset.orderid;
  },
  getOrdersNoRecvFromServer: function () {
    var _this = this;
    var urlNoPay = API.SHOP_SERVE.GET_ORDER_LIST + "/" + this.data.shopId + "/" + UTILS.ORDER_NOPAY;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    UTILS.httpGet(urlNoPay, header, function(data) {
      for (let index in data.result) {
        let orderTime = UTILS.formatTime(data.result[index].gmtModified.epochSecond, "h:m");
        _this.setData({
          ordersNoRecv: data.result,
          [`ordersNoRecv[${index}].orderTime`]: orderTime
        });
      }
    });
  },
  getOrdersWorkingFromServer: function () {
    var _this = this;
    var urlWaiting = API.SHOP_SERVE.GET_ORDER_LIST + "/" + this.data.shopId + "/" + UTILS.ORDER_PAY;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    UTILS.httpGet(urlWaiting, header, function(data) {
      for (let index in data.result) {
        let orderTime = UTILS.formatTime(data.result[index].gmtModified.epochSecond, "h:m");
        _this.setData({
          ordersWorking: data.result,
          [`ordersWorking[${index}].orderTime`]: orderTime
        });
      }
    });
  },
  getOrdersNoticeFromServer: function () {
    var _this = this;
    var urlHistory = API.SHOP_SERVE.GET_ORDER_LIST + "/" + this.data.shopId + "/" + UTILS.ORDER_FINISH;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    UTILS.httpGet(urlHistory, header, function(data) {
      for (let index in data.result) {
        let orderTime = UTILS.formatTime(data.result[index].gmtModified.epochSecond, "Y M D h:m");
        _this.setData({
          ordersNotice: data.result,
          [`ordersNotice[${index}].orderTime`]: orderTime
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: APP.globalData.shopId
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrdersNoRecvFromServer();
    this.getOrdersWorkingFromServer();
    this.getOrdersNoticeFromServer();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})